package com.xianshijian.oauth.userinfo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xianshijian.oauth.common.entity.OAuth;
import com.xianshijian.oauth.common.entity.OAuthResult;
import com.xianshijian.oauth.common.utils.ExceptionUtil;
import com.xianshijian.oauth.userinfo.service.UserInfoService;

/**
 * 请求用户信息接口.
 * 
 * @author lzj
 *
 */
@Controller
public class UserInfoController {

	@Autowired
	private UserInfoService userService;

	@RequestMapping(value = "/userinfo/{accessToken}", method = RequestMethod.GET)
	public @ResponseBody OAuthResult getUserInfo(@PathVariable String accessToken) {
		try {
			return userService.findUserByAccessToken(accessToken);
		} catch (Exception e) {
			e.printStackTrace();
			return OAuthResult.build(OAuth.StatusCode.SERVER_ERROR, ExceptionUtil.getStackTrace(e));
		}
	}

	@RequestMapping(value = "/checkAccessToken/{accessToken}", method = RequestMethod.GET)
	public @ResponseBody OAuthResult checkAccessToken(@PathVariable String accessToken) {
		try {
			return userService.checkAccessToken(accessToken);
		} catch (Exception e) {
			e.printStackTrace();
			return OAuthResult.build(OAuth.StatusCode.SERVER_ERROR, ExceptionUtil.getStackTrace(e));
		}
	}

	@RequestMapping(value = "/addAccessToken/{authorizationCode}/{accessToken}", method = RequestMethod.GET)
	public @ResponseBody OAuthResult addAccessToken(@PathVariable String authorizationCode, @PathVariable String accessToken) {
		try {
			return userService.addAccessToken(authorizationCode, accessToken);
		} catch (Exception e) {
			e.printStackTrace();
			return OAuthResult.build(OAuth.StatusCode.SERVER_ERROR, ExceptionUtil.getStackTrace(e));
		}
	}

	@RequestMapping(value = "/checkAuthCode/{authCode}", method = RequestMethod.GET)
	public @ResponseBody OAuthResult checkAuthCode(@PathVariable String authCode) {
		try {
			return userService.checkAuthCode(authCode);
		} catch (Exception e) {
			e.printStackTrace();
			return OAuthResult.build(OAuth.StatusCode.SERVER_ERROR, ExceptionUtil.getStackTrace(e));
		}
	}
	
	@RequestMapping(value = "/addAuthCode/{accountId}/{serviceId}/{authorizationCode}", method = RequestMethod.GET)
	public @ResponseBody OAuthResult addAuthCode(@PathVariable String accountId, @PathVariable String serviceId, @PathVariable String authorizationCode) {
		try {
			return userService.addAuthCode(accountId, serviceId, authorizationCode);
		} catch (Exception e) {
			e.printStackTrace();
			return OAuthResult.build(OAuth.StatusCode.SERVER_ERROR, ExceptionUtil.getStackTrace(e));
		}
	}

}
